<template>
  <div class="table-main">
    <el-dialog
      @close="handMyClose"
      :append-to-body="true"
      title="汽车服务"
      :visible.sync="dialogVisible1"
      width="60%"
      :close-on-click-modal="false"
    >
      <div style="margin-top:20px;">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane :label="item.title" :name="item.id" v-for="item in tabList" :key="item.id">
            <el-form
              :rules="rules"
              style="margin-top:20px;"
              ref="form"
              :model="form"
              label-width="120px"
              v-if="form.newList.length!=0"
            >
              <el-row
                v-for="(item,index) in form.newList"
                :key="item.id"
                :gutter="20"
                justify="center"
                type="flex"
              >
                <el-col :span="16">
                  <el-form-item
                    :rules="rules.valueNum"
                    :label="item.keyName+':'"
                    :prop="'newList.' + index + '.valueNum'"
                  >
                    <div style="display:flex;">
                      <el-input :disabled="true" v-model="item.valueNum" placeholder="请输入"></el-input>
                      <el-button
                        :disabled="item.flg||item.valueNum==0"
                        v-if="isShow!=3"
                        @click="handMinus(item)"
                        style="margin-left:10px"
                        type="primary"
                        icon="el-icon-minus"
                        circle
                      ></el-button>
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div
              v-if="form.newList.length==0"
              style="margin: 0 auto;text-align: center;"
              class="el-table__empty-text"
            >暂无数据</div>
          </el-tab-pane>
        </el-tabs>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button
          type="primary"
          @click="handFromSub('form')"
          :loading="bntLoading"
          v-if="isShow!=3"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { imgSizi } from '@/utils/myUtils'
export default {
  name: 'SidebarLogo',
  components: {},
  watch: {
    isState(newObj) {
      this.isShow = newObj
    }
  },
  props: {
    isState: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      activeName: '1',
      bntLoading: false,
      loading2: false,
      tabList: [
        {
          title: '购买服务',
          id: '1'
        },
        {
          title: '会员卡服务',
          id: '2'
        }
      ],
      rules: {
        valueNum: [
          {
            required: true,
            message: '请输入内容并且必须为数字',
            trigger: 'blur'
          },
          {
            validator(rule, value, callback) {
              var reg = /^[0-9]{1}([0-9]|[.])*$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入内容并且必须为数字'))
              }
            }
            // trigger: 'blur'
          }
        ]
      },
      headers: {
        token: localStorage.getItem('mytoken') || ''
      },
      form: {
        newList: []
      },
      shopStatusOptions: [],
      isShow: 0,
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible1: false,
      addressKeyword: '',
      dialogVisible: false,
      tipShow: false,
      findAllsList: [],
      findAllCarShopType: [],
      carShopGoodsFindAll: [],
      userVipDetail: [],
      carUserProject: [],
      newList: []
    }
  },
  async mounted() {},
  methods: {
    handMinus(row) {
      console.log(row.flg)
      if (!row.flg) {
        row.flg = true
        row.valueNum = parseInt(row.valueNum) - 1
      }

      console.log('查询轮播详情2', this.form.newList)
    },
    handleClick(val) {
      console.log(val)
      if (val.index == 1) {
        this.form.newList = this.carUserProject
      } else {
        this.form.newList = this.userVipDetail
      }
    },
    handDel(item, index) {
      console.log(item, index)
      this.form.vipDetails.splice(index, 1)
    },
    handEdit(item) {
      item.ifShow = false
      console.log(this.form)
    },
    handAdd() {
      this.form.vipDetails.push({
        keyName: '',
        valueNum: '',
        remaker: '',
        ifShow: true
      })
    },
    handSeleDel() {},
    uploadChange() {},
    onError() {},
    handGetShop(row) {
      console.log(row, 'row')
      let newForm = {
        carNum: row.carNum,
        userid: row.userId,
        shopid: row.shopId
      }
      this.$api.selectByIdCarUserProject(newForm).then(res => {
        // this.form = res.data.data
        this.carUserProject = res.data.data.userVipDetail
        res.data.data.carUserProject.forEach(item => {
          item.keyName = item.projectKey
          item.valueNum = item.projectValue
          item.flg = false
        })
        res.data.data.userVipDetail.forEach(item => {
          item.flg = false
        })
        this.userVipDetail = res.data.data.carUserProject
        this.form.newList = this.userVipDetail
      })
    },
    handSuccess(res, file) {
      var url = JSON.parse(res.message)
      this.$set(this.form, 'icon', url.url)
      this.loading2 = false
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isSize
    },
    handSuccess2(res, file) {
      var url = JSON.parse(res.message)
      this.$set(this.form, 'img', url.url)
      this.loading2 = false
    },
    beforeAvatarUpload2(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isSize
    },
    handMyClose() {
      for (let key in this.form) {
        this.form[key] = ''
      }
      this.$parent.closeFrom()
    },
    handFromSub(formName) {
      //1编辑
      console.log(this.$refs.form)
      if (this.isState == 1) {
        this.$refs.form[0].validate(valid => {
          if (valid) {
            this.userVipDetail.forEach(item => {
              item.projectValue = item.valueNum
            })
            let newForm = {
              carUserProject: JSON.stringify(this.userVipDetail),
              userVipDetail: JSON.stringify(this.carUserProject)
            }
            this.bntLoading = true
            this.$api
              .updateCarUserProject(newForm)
              .then(res => {
                this.dialogVisible1 = false
                this.$parent.init()
                console.log(res, '编辑', this.form)
                this.$message({
                  message: res.data.message,
                  type: 'success'
                })
                this.bntLoading = false
              })
              .finally(() => {
                this.bntLoading = false
              })
          } else {
            console.log('error submit!!')
            this.$message({
              message: '请完成表单录入'
            })
            this.bntLoading = false
            return false
          }
        })
      } else if (this.isState == 0) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            // this.form.vipDetails = JSON.stringify(this.form.vipDetails)
            if (this.form.isGive == 0) {
              this.form.goodName = ''
              this.form.giveNum = ''
            }
            this.bntLoading = true
            this.$api
              .addCarShopGoods(this.form)
              .then(res => {
                this.dialogVisible1 = false
                this.bntLoading = false
                this.$parent.init()
                this.$refs.form.resetFields()
                console.log(res, '添加,表单验证', this.form)
                this.$message({
                  message: res.data.message,
                  type: 'success'
                })
              })
              .finally(() => {
                this.bntLoading = false
              })
          } else {
            console.log('error submit!!')
            this.$message({
              message: '请完成表单录入'
            })
            return false
          }
        })
      }
    },

    // handSuccess(response, file, fileList) {
    //   console.log(response, file, fileList, '成功回调')
    //   this.form.shopImg = response.message
    // },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    // handlePictureCardPreview(file) {
    //   this.dialogImageUrl = file.url
    //   this.dialogVisible2 = true
    // },
    handleClose() {
      this.dialogVisible = false
    },
    handMapShow() {
      this.dialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
.vipProjeck {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .pro_item {
    width: 20%;
  }
}
.map_btn {
  width: 82px;
  height: 22px;
  background: rgba(56, 173, 147, 1);
  border-radius: 4px;
  color: #fff;
  font-size: 14px;
  line-height: 22px;
  text-align: center;
  margin-top: 8px;
}
/deep/ .el-form-item__label {
  font-size: 14px;
  color: #000;
  font-weight: bold;
}
.form_box {
  width: 80%;
  margin: 0 auto;
}
.avatar-uploader /deep/ .el-upload {
  border: 1px dashed #38ad93;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #38ad93;
  width: 56px;
  height: 44px;
  line-height: 44px;
  text-align: center;
}
.avatar {
  width: 56px;
  height: 44px;
  display: block;
}
.avatar-uploader-icon2 {
  font-size: 28px;
  color: #38ad93;
  width: 351px;
  height: 192px;
  line-height: 192px;
  text-align: center;
}
.avatar2 {
  width: 351px;
  height: 192px;
  display: block;
}
</style>
